package com.intel.factorytest.cfparser.concrete;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.intel.factorytest.menuitem.TestsItemInformation;
import com.intel.factorytest.menuitem.TestsItemInformationCN;
import com.intel.factorytest.util.PropertiesUtil;

public class PropertiesConfParser extends AConfigFileParser {
    
    private static final String TEST_ON = "1";
    private static final String TEST_OFF = "0";
    
    private static final String TC_VERSION_TEST="版本测试";//add by wangsm
    private static final String TC_BACKLIGHT_TEST="背光测试";//add by wangsm
    private static final String TC_TORCH_TEST="手电筒测试";//add by wangsm
    private static final String TC_RTC = "RTC";//add by wangsm
    private static final String TC_BLUETOOTH = "蓝牙测试";//add by wangsm
    private static final String TC_WIFI = "Wifi测试";//add by wangsm
    private static final String TC_SoftwareInfo = "软件信息";
    private static final String TC_SimCard = "Sim卡";
    private static final String TC_TouchPanel = "触摸屏测试";
    private static final String TC_TouchScreen = "电子笔测试";
    private static final String TC_Display = "显示";
    private static final String TC_Illumination = "亮度";
    private static final String TC_VolumeControl = "扬声器和耳机测试";
    private static final String TC_KbSwitch = "按键测试";
    private static final String TC_Sensors = "传感器";
    private static final String TC_HDMI = "HDMI";
    private static final String TC_Vibrator = "振动";
    private static final String TC_Comprehensive = "综合测试";
    private static final String TC_BatteryStatusCheck = "电池状态检验";//wangsm
//    
    private static final String TC_ExternalCard = "内部/外部存储器";//wangsm
    private static final String TC_Speaker = "扬声器";//modify by wangsm 左右声道
    private static final String TC_Usb = "USB";
    private static final String TC_OTG = "OTG(鼠标,U盘)";
    private static final String TC_XLink = "XLink";
    private static final String TC_XLink_OTG = "XLink OTG test";
    private static final String TC_SARSensor = "SARSensor";
    private static final String TC_Microphone = "麦克风";
    private static final String TC_MicrophoneSub = "副麦克风";
    private static final String TC_MicrophoneShow = "麦克风频谱";
    private static final String TC_HeadsetMicrophone = "耳机麦克风";
    private static final String TC_Video = "视频";
//    private static final String TC_Audio = "音频";
    private static final String TC_BackCamera = "后置照相机";
    private static final String TC_FrontCamera = "前置照相机";
    private static final String TC_CameraWhole = "摄像头";
    private static final String TC_SystemCamera = "系统照相机";
//    private static final String TC_Bluetooth = "蓝牙";
//    private static final String TC_WIFI = "WIFI";
    private static final String TC_WIFI_Bluetooth = "WIFI和蓝牙";
    private static final String TC_GPSHot = "GPS";
    private static final String TC_Call = "通话测试";
    private static final String TC_FM = "FM测试";
    private static final String TC_Receiver = "听筒测试";
    private static final String TC_Pogo = "Pogo键盘测试";
    private static final String TC_LED = "LED";
    private static final String TC_NFC = "NFC";
    private static final String TC_Gyroscope = "GyroscopeTest";
    private static final String TC_CompassTest = "CompassTest";
    
    
    private PropertiesUtil propertiesUtil;
    
    public static Map<String, String> TestCaseName = new HashMap<String, String>();
    static {
        TestCaseName.put(TC_VERSION_TEST, "System Info");//add by wangsm
        TestCaseName.put(TC_SoftwareInfo, "Software Information");
        TestCaseName.put(TC_SimCard, "Nano Sim Card");
        TestCaseName.put(TC_TouchPanel, "Touch screen");
        TestCaseName.put(TC_TouchScreen, "Touch Screen");
        TestCaseName.put(TC_Display, "Display");
        TestCaseName.put(TC_BACKLIGHT_TEST, "Backlight test");//add by wangsm
        TestCaseName.put(TC_Illumination, "Brightness");
        TestCaseName.put(TC_TORCH_TEST, "Camera back Flash");//add by wangsm
        TestCaseName.put(TC_RTC, "RTC");//add by wangsm
        TestCaseName.put(TC_BLUETOOTH, "Bluetooth test");//add by wangsm
        TestCaseName.put(TC_WIFI, "Wifi test");//add by wangsm
        TestCaseName.put(TC_VolumeControl, "Speaker & Headset");
        TestCaseName.put(TC_KbSwitch, "Keyboard & switch");
        TestCaseName.put(TC_Sensors, "Sensors");
        TestCaseName.put(TC_HDMI, "HDMI");
        TestCaseName.put(TC_Vibrator, "Vibrator");
        TestCaseName.put(TC_Comprehensive, "ComprehensiveTest");
        TestCaseName.put(TC_BatteryStatusCheck, "Battery");//wangsm
        TestCaseName.put(TC_ExternalCard, "Micro SD Card");//wangsm
        TestCaseName.put(TC_Speaker, "Speaker music");
        TestCaseName.put(TC_Usb, "USB");
        TestCaseName.put(TC_OTG, "OTG(Mouse,Udisk)");
        TestCaseName.put(TC_XLink, "XLink");
        TestCaseName.put(TC_XLink_OTG, "XLink OTG test");
        TestCaseName.put(TC_SARSensor, "SARSensor");
        TestCaseName.put(TC_Microphone, "Bottom Front mic");
        TestCaseName.put(TC_MicrophoneSub, "Back mic");
        TestCaseName.put(TC_MicrophoneShow, "MicrophoneShow");
        TestCaseName.put(TC_HeadsetMicrophone, "Audio Jack");
        TestCaseName.put(TC_Video, "Video");
//        TestCaseName.put(TC_Audio, "Audio");
        TestCaseName.put(TC_BackCamera, "Back Camera");
        TestCaseName.put(TC_FrontCamera, "Front Camera");
        TestCaseName.put(TC_CameraWhole, "Camera");
        TestCaseName.put(TC_SystemCamera, "System Camera");
//        TestCaseName.put(TC_Bluetooth, "Bluetooth");
//        TestCaseName.put(TC_WIFI, "WLAN");
        TestCaseName.put(TC_WIFI_Bluetooth, "WIFI & Bluetooth");
        TestCaseName.put(TC_GPSHot, "GPS Location");
        TestCaseName.put(TC_Call, "Call Test");
        TestCaseName.put(TC_FM, "FM Test");
        TestCaseName.put(TC_Receiver, "Earpiece Music");
        TestCaseName.put(TC_Pogo, "Pogo");        
        TestCaseName.put(TC_LED, "LED");         
        TestCaseName.put(TC_NFC, "NFC");         
        TestCaseName.put(TC_Gyroscope, "GyroscopeTest");
        TestCaseName.put(TC_CompassTest, "CompassTest");
    }

    public PropertiesConfParser(Context context) {
        super(context);
        propertiesUtil = new PropertiesUtil();
    }
    
    @Override
    protected List<Map<String, Object>> readConfigFile(InputStream in,
            int isSys,int isSysForPcba, String localeString, String sdPath) {
        List<Map<String, Object>> testItemMenu = null;
        boolean bSysTest = isSys == 1 ? true : false;
        if (bSysTest) {
            testItemMenu = initTestcaseForSystemTest(in, localeString);
        } else {
            testItemMenu = initTestcaseForSingleTest(localeString);
        }
        return testItemMenu;
    }

    private List<Map<String, Object>> initTestcaseForSystemTest(InputStream in, String localeString) {
        List<Map<String, Object>> testItemMenu = new ArrayList<Map<String, Object>>();
        // load properties
        loadProperties(in);
        
        //在properties中的key就等于TestsItemInformationCN中的item_name
        List<Object> props = propertiesUtil.getKeyList();
        String toFindKey = null;
        for (Object obj : props) {
            String key = (String) obj;
            String value = (String) propertiesUtil.getProperty(key, TEST_OFF);
            Set<TestsItemInformation> setEN = null;
            Set<TestsItemInformationCN> setCN = null;
            Iterator it1 = null;
            setCN = Collections.synchronizedSet(EnumSet.allOf(TestsItemInformationCN.class));
            it1 = setCN.iterator();
            if(localeString.equals("CN")) {
                setCN = Collections.synchronizedSet(EnumSet.allOf(TestsItemInformationCN.class));
                it1 = setCN.iterator();
            } else {
                setEN = Collections.synchronizedSet(EnumSet.allOf(TestsItemInformation.class));
                it1 = setEN.iterator();
            }
            while (it1.hasNext()) {
                TestsItemInformation l = null;
                TestsItemInformationCN lCN = null;
                if (localeString.equals("CN")) {
                    lCN = (TestsItemInformationCN) it1.next();
                    toFindKey = TestCaseName.get(lCN.item_name);
                } else {
                    l = (TestsItemInformation) it1.next();
                    toFindKey = l.item_name;
                }
                
                if (key.equals(toFindKey)) {
                    if (value.equals(TEST_OFF)) {
                        continue;
                    } else {
                        Map<String, Object> testItem = new HashMap<String,Object>();
                        Intent bIntent = new Intent();
                        if(localeString.equals("CN")) {
                            bIntent.setClassName(lCN.package_name, lCN.class_name);
                            bIntent.putExtra("value", value);
                            testItem.put("itemName", lCN.item_name);
                        } else {
                            bIntent.setClassName(l.package_name, l.class_name);
                            bIntent.putExtra("value", value);
                            testItem.put("itemName", l.item_name);
                        }
                        testItem.put("intent", bIntent);
                        testItemMenu.add(testItem);
                    }
                }
                
            }
        }
        return testItemMenu;
    }
    
    private List<Map<String, Object>> initTestcaseForSingleTest(String localeString) {
        List<Map<String, Object>> testItemMenu = new ArrayList<Map<String, Object>>();

        Set<TestsItemInformation> setEN = null;
        Set<TestsItemInformationCN> setCN = null;
        Iterator it1 = null;
        setCN = Collections.synchronizedSet(EnumSet.allOf(TestsItemInformationCN.class));
        if (localeString.equals("CN")) {
            setCN = Collections.synchronizedSet(EnumSet.allOf(TestsItemInformationCN.class));
            it1 = setCN.iterator();
        } else {
            setEN = Collections.synchronizedSet(EnumSet.allOf(TestsItemInformation.class));
            it1 = setEN.iterator();
        }
        while (it1.hasNext()) {
            TestsItemInformation l = null;
            TestsItemInformationCN lCN = null;
            if (localeString.equals("CN")) {
                lCN = (TestsItemInformationCN) it1.next();
            } else {
                l = (TestsItemInformation) it1.next();
            }

            Map<String, Object> testItem = new HashMap<String, Object>();
            Intent bIntent = new Intent();
            if (localeString.equals("CN")) {
                bIntent.setClassName(lCN.package_name, lCN.class_name);
                bIntent.putExtra("value", TEST_ON);
                testItem.put("itemName", lCN.item_name);
            } else {
                bIntent.setClassName(l.package_name, l.class_name);
                bIntent.putExtra("value", TEST_ON);
                testItem.put("itemName", l.item_name);
            }
            testItem.put("intent", bIntent);
            testItemMenu.add(testItem);
        }
        return testItemMenu;
    }

    private void loadProperties(InputStream in) {
        try {
            propertiesUtil.load(in);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    
    public PropertiesUtil getProperties(boolean isPcba) {
        loadProperties(loadConfigFile(isPcba));
        return propertiesUtil;
    }

}
